package com.qqtech.qquan.sys.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.sys.dao.SysFunctionDao;
import com.qqtech.qquan.sys.model.SysFunction;
import com.qqtech.qquan.sys.model.SysFunctionQuery;
import com.qqtech.qquan.sys.model.SysFunctionVo;

@Service
public class SysFunctionServiceImpl extends BaseServiceImpl<SysFunction>
		implements SysFunctionService {
	public static final Logger log = LoggerFactory.getLogger(SysFunctionServiceImpl.class);
	
	@Resource
	private SysFunctionDao sysFunctionDao;

	@Override
	protected BaseDao<SysFunction> getBaseDao() {
		return sysFunctionDao;
	}

	@Override
	public List<SysFunctionVo> queryListByParentId(SysFunctionQuery sysFunction) {
		return sysFunctionDao.queryListByParentId(sysFunction);
	}

	@Override
	public Integer getMaxOrderIdx() {
		Integer maxOrderIdx = sysFunctionDao.getMaxOrderIdx();
		if (null == maxOrderIdx) {
			maxOrderIdx = 0;
		}
		return maxOrderIdx;
	}

	@Override
	public int move(Integer targetId, Integer sourceId, String point) {
		SysFunctionVo target =sysFunctionDao.getById(targetId);
		SysFunctionVo source = sysFunctionDao.getById(sourceId);
		if (target.getType().intValue() == 2 && point.equals("append")) { //叶子节点不允许追加子节点
			return 0;
		}
		Integer maxOrderIdx = sysFunctionDao.getMaxOrderIdx();
		Integer nextOrderIdx = 1;
		if (null != maxOrderIdx) {
			nextOrderIdx = maxOrderIdx + 1;
		}
		if(point.equals("append")) {				//如果是追加操作,那么直接追加到父节点下
			source.setParentId(target.getId());
			source.setOidx(nextOrderIdx);
			sysFunctionDao.updateById(source);
		} else {									//否则是排序操作,那么移动为与目标节点同级,再执行同级排序
			source.setParentId(target.getParentId());
			source.setOidx(nextOrderIdx);
			sysFunctionDao.updateById(source);
			sortForMove(target, source, point);
		}
		return 1;
	}
	
	/**
	 * 为移动操作排序
	 * @param target
	 * @param source
	 * @param point
	 */
	private void sortForMove(SysFunctionVo target, SysFunctionVo source,String point) {
		SysFunctionQuery query = new SysFunctionQuery();
		query.setParentId(target.getParentId());
		List<SysFunctionVo> children = this.queryListByParentId(query);
		int index = 0;
		for(;index<children.size(); index++) {
			SysFunctionVo m = children.get(index);
			if (target.getId().intValue() == m.getId()) {
				if(point.equals("top")) {			//插入源节点到适当的位置
					children.add(index,source);
				} else if(point.equals("bottom")) {
					children.add(index+1,source);
					index=index+1;					//保存源节点的索引号
				}
				break;
			}
		}
		children.remove(children.size()-1);			//删除重复的源节点(固定为最后一个节点),因为前面循环中,插入了一个源节点
		
		Integer orderIdx = source.getOidx();		//保存源节点的排序号
		for(;index<children.size();index++) {		//重新设置源节点和之后的节点排序号
			SysFunctionVo m = children.get(index);
			if(index<children.size()-1) {
				m.setOidx(children.get(index+1).getOidx());
			} else {
				m.setOidx(orderIdx);
			}
			sysFunctionDao.updateById(m);
		}
	}

	@Override
	public List<SysFunctionVo> queryChildrenList(Integer parentId,
			Integer status) {
		SysFunctionQuery sysFunction = new SysFunctionQuery();
		sysFunction.setParentId(parentId);
		sysFunction.setStatus(status);
		return sysFunctionDao.queryListByParentId(sysFunction);
	}

	@Override
	public List<SysFunctionVo> queryChildrenForRoleList(String roleIds,
			Integer parentId, Integer status) {
		SysFunctionQuery sysFunction = new SysFunctionQuery();
		sysFunction.setParentId(parentId);
		sysFunction.setStatus(status);
		sysFunction.setRoleIds(roleIds);
		return sysFunctionDao.getChildrenForRole(sysFunction);
	}
	
}